import { LoginData, LoginResult, VerifyCodeResult, TenantInfo } from '@/api/auth.type'
import { UserInfo } from '@/api/system/types/user.type'

/**
 * pc端固定客户端授权id
 */
const clientId = import.meta.env.VITE_APP_CLIENT_ID

/**
 * @param data {LoginData}
 * @returns
 */
export function login(data: LoginData): Promise<ApiResponse<LoginResult>> {
  const params = {
    ...data,
    clientId: data.clientId || clientId,
    grantType: data.grantType || 'password'
  }
  return uni.$uv.http.request({
    url: '/auth/login',
    header: {
      isToken: false,
      isEncrypt: true
    },
    method: 'post',
    data: params
  })
}

/**
 * 获取验证码
 */
export function getCodeImg(): Promise<ApiResponse<VerifyCodeResult>> {
  return uni.$uv.http.request({
    url: '/auth/code',
    header: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}

/**
 * 注销
 */
export function logout() {
  return uni.$uv.http.post('/auth/logout')
}

/**
 * 获取用户详细信息
 */
export function getInfo(): Promise<ApiResponse<UserInfo>> {
  return uni.$uv.http.request({
    url: '/system/user/getInfo',
    method: 'get'
  })
}

/**
 * 获取租户列表
 */
export function getTenantList(): Promise<ApiResponse<TenantInfo>> {
  return uni.$uv.http.request({
    url: '/auth/tenant/list',
    header: {
      isToken: false
    },
    method: 'get'
  })
}
